unit PoslovniPartnerRDM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, StdVcl, FMTBcd, Provider, alProvider, DB, SqlExpr,
  PoslovanjeSrv_TLB,
  SifrantRDM;

type
  TrdmPoslovniPartner = class(TrdmSifrant, IrdmPoslovniPartner)
    dsRacun: TSQLDataSet;
    dspRacun: TBaseDataSetProvider;
  private
    { Private declarations }
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
    function GetSelectCommandText(DataSet: TSQLDataSet): string; override;
    function Get_RazredID(const ProviderName: WideString): Integer; safecall;
    function GetTableName: string; override;
    procedure RefreshRecord(const Session: ISession;
      const ProviderName: WideString; ID: Integer); safecall;
    procedure Renumber(const Session: ISession; const FieldName,
      SortFieldName: WideString; StartValue, EndValue: Integer); safecall;
    procedure SetDefaultExpression(DataSet: TDataSet); override;
    procedure Set_ReadOnly(Value: WordBool); safecall;
    procedure Set_SQLConnection(Value: Integer); safecall;
  public
    { Public declarations }
  end;

var
  PoslovniPartnerFactory: TComponentFactory;

implementation

{$R *.DFM}

function TrdmPoslovniPartner.GetSelectCommandText(
  DataSet: TSQLDataSet): string;
var
  SQL: TStringList;
begin
  SQL := TStringList.Create;
  try
    if DataSet = dsMain then
    begin
      SQL.Add('select');
        SQL.Add('PPA.*,');
        SQL.Add('POS.SIFRA as POSTA_SIFRA,');
        SQL.Add('POS.NAZIV as POSTA_NAZIV,');
        SQL.Add('SPP.NAZIV as SKUPINA_NAZIV,');
        SQL.Add('PST.NAZIV as PRAVNI_STATUS_NAZIV');
      SQL.Add('from POSLOVNI_PARTNER PPA');
      SQL.Add('inner join SKUPINA_POSLOVNI_PARTNER SPP on (PPA.SKUPINA_ID = SPP.ID)');
      SQL.Add('inner join PRAVNI_STATUS PST on (PPA.PRAVNI_STATUS_ID = PST.ID)');
      SQL.Add('inner join POSTA POS on (PPA.POSTA_ID = POS.ID)');
    end
    else if DataSet = dsRacun then
    begin
      SQL.Add('select');
        SQL.Add('PPR.*');
      SQL.Add('from POSLOVNI_PARTNER_RACUN PPR');
    end;
  finally
    Result := Trim(SQL.Text);
    SQL.Free;
  end;
end;

function TrdmPoslovniPartner.GetTableName: string;
begin
  Result := 'PPA';
end;

function TrdmPoslovniPartner.Get_RazredID(
  const ProviderName: WideString): Integer;
begin
  Result := inherited Get_RazredID(ProviderName);
end;

procedure TrdmPoslovniPartner.RefreshRecord(const Session: ISession;
  const ProviderName: WideString; ID: Integer);
begin
  inherited;
end;

procedure TrdmPoslovniPartner.Renumber(const Session: ISession;
  const FieldName, SortFieldName: WideString; StartValue,
  EndValue: Integer);
begin
  inherited;
end;

procedure TrdmPoslovniPartner.SetDefaultExpression(DataSet: TDataSet);
begin
  inherited;
  if DataSet = dsMain then
  begin
    with DataSet as TSQLDataSet do
    begin
      FieldByName('DAVCNI_ZAVEZANEC').DefaultExpression := '''N''';
      FieldByName('DAVCNA_STEVILKA').DefaultExpression := '''0''';
      FieldByName('MATICNA_STEVILKA').DefaultExpression := '''0''';
      FieldByName('ROK_PLACILA').DefaultExpression := '0';
    end
  end;
end;

procedure TrdmPoslovniPartner.Set_ReadOnly(Value: WordBool);
begin
  inherited;
end;

procedure TrdmPoslovniPartner.Set_SQLConnection(Value: Integer);
begin
  inherited;
end;

class procedure TrdmPoslovniPartner.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

initialization
  PoslovniPartnerFactory := TComponentFactory.Create(ComServer, TrdmPoslovniPartner,
    Class_rdmPoslovniPartner, ciInternal, tmApartment);
end.
